package sw6.admin.gui.types;

import sw6.admin.R;
import sw6.lib.Settings;
import sw6.lib.types.Interval;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class IntervalGui extends Activity{

	Interval mInterval;
	String mVarName;
	String mPackageName;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.interval);

		Bundle extra = getIntent().getExtras();

		mVarName = extra.getString(Settings.OBJECT_ATTRIBUTE_VARNAME);
		mPackageName = extra.getString(Settings.OBJECT_ATTRIBUTE_PACKAGENAME);
		String description = extra.getString(Settings.OBJECT_ATTRIBUTE_DESCRIPTION);
		String realName = extra.getString(Settings.OBJECT_ATTRIBUTE_REALNAME);

		mInterval = Settings.getStdObject(IntervalGui.this, mPackageName, mVarName, Interval.class);

		TextView title = (TextView) this.findViewById(R.id.title);
		TextView desc = (TextView) this.findViewById(R.id.desc);

		title.setText(realName);
		desc.setText(description);
		
		this.setTitle(realName);

		realName = "";
		if (mInterval == null) {
			displayErrorDialog("This setting is not available. This issue is most likely caused because the application using this setting has not been started yet. Solve the problem by starting the application. If you still get this error, it is a problem with the application.");
		}
		else
		{
			setupButtons();
			setupSeekbars();
		}

		//extra.getString("")
	}

	private void setupButtons() {
		Button okButton = (Button)this.findViewById(R.id.ok);
		Button fortrydButton = (Button)this.findViewById(R.id.cancel);

		final IntervalGui intervalGui = this;

		okButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				Settings.setStdObject(getApplicationContext(), mPackageName, mVarName, mInterval);
				intervalGui.finish();	
			}});

		fortrydButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				intervalGui.finish();
			} });
	}

	private void setupSeekbars() {
		SeekBar seekbar1 = (SeekBar)this.findViewById(R.id.seekBar1);
		SeekBar seekbar2 = (SeekBar)this.findViewById(R.id.seekBar2);
		final TextView number1 = (TextView)this.findViewById(R.id.value1);
		final TextView number2 = (TextView)this.findViewById(R.id.value2);

		seekbar1.setMax(mInterval.getMax() - mInterval.getMin());
		seekbar2.setMax(mInterval.getMax() - mInterval.getMin());

		seekbar1.setProgress(mInterval.getFrom() - mInterval.getMin());
		seekbar2.setProgress(mInterval.getTo() - mInterval.getMin());

		number1.setText(Integer.toString(mInterval.getFrom()));
		number2.setText(Integer.toString(mInterval.getTo()));

		seekbar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {

				int value = progress + mInterval.getMin();

				if (value <= mInterval.getTo()) {
					number1.setText(Integer.toString(value));
					mInterval.set(value, mInterval.getTo());
				}
				else if (fromUser == true) {
					seekBar.setProgress(mInterval.getTo() - mInterval.getMin());
				}
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {}
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {}}
		);

		seekbar2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				int value = progress + mInterval.getMin();

				if (value >= mInterval.getFrom()) {
					number2.setText(Integer.toString(value));
					mInterval.set(mInterval.getFrom(), value);
				}
				else if (fromUser == true) {
					seekBar.setProgress(mInterval.getFrom() - mInterval.getMin());
				}
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {}
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {}}
		);
	}

	private void displayErrorDialog(String error) {

		Dialog errorDialog = new Dialog(IntervalGui.this);
		errorDialog.setTitle("Fejl");
		errorDialog.setContentView(R.layout.error_dialog);

		TextView textView = (TextView) errorDialog.findViewById(R.id.textView1);
		Button okButton = (Button) errorDialog.findViewById(R.id.button1);

		textView.setText(error);

		final IntervalGui intervalGui = this;
		okButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				intervalGui.finish();
			}});

		errorDialog.show();
	}
}
